package com.jic.market.mapper.base;

import com.jic.market.entity.base.BaseActivity;
import com.jic.market.impl.rest.discountamount.po.PromotionDataPo;

import com.jic.market.request.CouponConfigQueryRequest;
import com.jic.market.response.CouponConfigQueryResponse;
import com.jic.market.response.rest.coupon.QueryCouponCenterResponse;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * 基础活动表
 *
 * @author : seven
 * @version : v1.0.0
 * @email : 774329481@qq.com
 * @since : 2020-03-10 04:23:18
 */
@Repository
public interface BaseActivityMapper {

    long insert(BaseActivity baseActivity);

    long delete(@Param("promotionId") String promotionId);

    long update(BaseActivity baseActivity);

    BaseActivity queryByPrimaryKey(Long id);

    List<BaseActivity> query(BaseActivity baseActivity);

    List<BaseActivity> queryPage(@Param("startIndex") int startIndex, @Param("limit") int limit, @Param("baseActivity") BaseActivity baseActivity);

    long queryPageCount(@Param("baseActivity")BaseActivity baseActivity);

    @Select("select * from market_base_activity where promotion_id=#{promotionId} and delete_flag='0'")
    BaseActivity queryByPromotionId(@Param("promotionId")String promotionId);

    @Select("select * from market_base_activity where promotion_id=#{promotionId} and prom_type=#{promType} and delete_flag='0'")
    BaseActivity queryByPromotionIdAndPromType(@Param("promotionId")String promotionId,@Param("promType")String promType);

    @Update("update market_base_activity set status=#{status} where promotion_id=#{promotionId} and delete_flag='0'")
    long updateStatusByPromotionId(@Param("promotionId")String promotionId,
                                   @Param("status")String status);

    @Update("update market_base_activity set status=#{status},check_remark=#{checkRemark} where promotion_id=#{promotionId} and delete_flag='0'")
    long check(@Param("promotionId") String promotionId,
               @Param("checkRemark") String checkRemark,
               @Param("status") String status);



    @Select({
            "<script>",
            "select ma.* from market_base_activity ma",
            "where 1=1 and ma.delete_flag='0' and ma.status != '10' ",
            "<trim suffixOverrides=\"and\"> ",
            "<if test=\"baseActivity.promotionId !=null and baseActivity.promotionId != '' \">and ma.promotion_id = #{baseActivity.promotionId} </if>  ",
            "<if test=\"baseActivity.promName !=null\">and ma.prom_name like CONCAT(CONCAT('%', #{baseActivity.promName}), '%') </if>  ",
            "<if test=\"baseActivity.promType !=null\">and ma.prom_type = #{baseActivity.promType} </if>  ",
            "<if test=\"baseActivity.frontPromType !=null\">and ma.front_prom_type = #{baseActivity.frontPromType} </if>  ",
            "<if test=\"baseActivity.creatorName !=null and baseActivity.creatorName != '' \">and ma.creator_name like CONCAT(CONCAT('%', #{baseActivity.creatorName}), '%')  </if>  ",
            "<if test=\"baseActivity.startTime !=null\">and ma.start_time &gt;= #{baseActivity.startTime} </if>  ",
            "<if test=\"baseActivity.endTime !=null\">and ma.end_time &lt;= #{baseActivity.endTime} </if>  ",
            "<if test=\"baseActivity.channelCodes !=null\">and ma.channel_codes like CONCAT(CONCAT('%', #{baseActivity.channelCodes}), '%') </if>  ",
            "<if test=\"baseActivity.deptType !=null and baseActivity.deptType == 2\"> ",
            "   and (ma.dept_id = #{baseActivity.adminOperate.deptId} or ma.promotion_id in (" +
                    "select DISTINCT a.promotion_id from market_activity_merchant_config a " +
                    "left join market_base_activity b on a.promotion_id = b.promotion_id where a.merchant_id=#{baseActivity.adminOperate.businessId} and a.delete_flag='0' and b.status = 11)  "+
                    ")",
            "</if>  ",
            "<if test=\"baseActivity.deptType !=null and baseActivity.deptType == 3\"> ",
            "   and (ma.dept_id = #{baseActivity.adminOperate.deptId} or ma.promotion_id in (" +
                    "select DISTINCT a.promotion_id from market_activity_merchant_config a " +
                    "left join market_base_activity b on a.promotion_id = b.promotion_id where a.merchant_id=#{baseActivity.adminOperate.businessId} and a.delete_flag='0' and b.status = 11)  "+
                    ")",
            "</if> ",
            "<if test=\"baseActivity.deptType == 0\"> ",
                "and ma.dept_id = #{baseActivity.adminOperate.deptId}",
            "</if>",
            "<if test=\"baseActivity.deptType == 1\"> ",
                "and (ma.dept_id = #{baseActivity.adminOperate.deptId} or (ma.channel_codes like concat('%',#{baseActivity.adminOperate.businessId},'%') and ma.status = 11)) ",
            "</if>",
            "<if test=\"baseActivity.status == 9 or baseActivity.status == 7\">and ma.status = #{baseActivity.status} </if>  ",
            "<if test=\"baseActivity.status == 1 \">and ma.status = 1 and now() &lt;= ma.start_time </if>  ",
            "<if test=\"baseActivity.status == 2 \">and ma.status = 2 and now() &lt;= ma.start_time </if>  ",
            "<if test=\"baseActivity.status == 3 \">and ma.status = 11 and now() &lt;= ma.start_time </if>  ",
            "<if test=\"baseActivity.status == 4 \">and ma.status = 4 and now() &lt;= ma.start_time </if>  ",
            "<if test=\"baseActivity.status == 5 \">and ma.status = 11 and now() &gt;= ma.start_time and now() &lt;= ma.end_time </if>  ",
            "<if test=\"baseActivity.status == 8 \">and ma.status = 11 and now() &gt; ma.end_time </if>  ",
            "<if test=\"baseActivity.status == 6 \">and ma.status in(1,2,4) and now() &gt; ma.start_time </if>  ",
            "</trim>",
            "order by ma.id desc limit #{startIndex},#{limit} ",
            "</script>"
    })
    List<BaseActivity> querySkillPage(@Param("startIndex") int startIndex,
                                      @Param("limit") int limit,
                                      @Param("baseActivity") BaseActivity baseActivity,
                                      @Param("merchantId")String merchantId,
                                      @Param("storeId")String storeId);


    @Select({
            "<script>",
            "select count(ma.id) from market_base_activity ma",
            "where 1=1 and ma.delete_flag='0' and ma.status != '10' ",
            "<trim suffixOverrides=\"and\"> ",
            "<if test=\"baseActivity.promotionId !=null and baseActivity.promotionId != '' \">and ma.promotion_id = #{baseActivity.promotionId} </if>  ",
            "<if test=\"baseActivity.promName !=null\">and ma.prom_name like CONCAT(CONCAT('%', #{baseActivity.promName}), '%') </if>  ",
            "<if test=\"baseActivity.promType !=null\">and ma.prom_type = #{baseActivity.promType} </if>  ",
            "<if test=\"baseActivity.frontPromType !=null\">and ma.front_prom_type = #{baseActivity.frontPromType} </if>  ",
            "<if test=\"baseActivity.creatorName !=null and baseActivity.creatorName != '' \">and ma.creator_name like CONCAT(CONCAT('%', #{baseActivity.creatorName}), '%')  </if>  ",
            "<if test=\"baseActivity.startTime !=null\">and ma.start_time &gt;= #{baseActivity.startTime} </if>  ",
            "<if test=\"baseActivity.endTime !=null\">and ma.end_time &lt;= #{baseActivity.endTime} </if>  ",
            "<if test=\"baseActivity.channelCodes !=null\">and ma.channel_codes like CONCAT(CONCAT('%', #{baseActivity.channelCodes}), '%') </if>  ",
            "<if test=\"baseActivity.deptType !=null and baseActivity.deptType == 2\"> ",
            "   and (ma.dept_id = #{baseActivity.adminOperate.deptId} or ma.promotion_id in (" +
                    "select DISTINCT a.promotion_id from market_activity_merchant_config a " +
                    "left join market_base_activity b on a.promotion_id = b.promotion_id where a.merchant_id=#{baseActivity.adminOperate.businessId} and a.delete_flag='0' and b.status = 11)  "+
                    ")",
            "</if>  ",
            "<if test=\"baseActivity.deptType !=null and baseActivity.deptType == 3\"> ",
            "   and (ma.dept_id = #{baseActivity.adminOperate.deptId} or ma.promotion_id in (" +
                    "select DISTINCT a.promotion_id from market_activity_merchant_config a " +
                    "left join market_base_activity b on a.promotion_id = b.promotion_id where a.merchant_id=#{baseActivity.adminOperate.businessId} and a.delete_flag='0' and b.status = 11)  "+
                    ")",
            "</if> ",
            "<if test=\"baseActivity.deptType == 0\"> ",
            "and ma.dept_id = #{baseActivity.adminOperate.deptId}",
            "</if>",
            "<if test=\"baseActivity.deptType == 1\"> ",
            "and (ma.dept_id = #{baseActivity.adminOperate.deptId} or (ma.channel_codes like concat('%',#{baseActivity.adminOperate.businessId},'%') and ma.status = 11)) ",
            "</if>",
            "<if test=\"baseActivity.status == 9 or baseActivity.status == 7\">and ma.status = #{baseActivity.status} </if>  ",
            "<if test=\"baseActivity.status == 1 \">and ma.status = 1 and now() &lt;= ma.start_time </if>  ",
            "<if test=\"baseActivity.status == 2 \">and ma.status = 2 and now() &lt;= ma.start_time </if>  ",
            "<if test=\"baseActivity.status == 3 \">and ma.status = 11 and now() &lt;= ma.start_time </if>  ",
            "<if test=\"baseActivity.status == 4 \">and ma.status = 4 and now() &lt;= ma.start_time </if>  ",
            "<if test=\"baseActivity.status == 5 \">and ma.status = 11 and now() &gt;= ma.start_time and now() &lt;= ma.end_time </if>  ",
            "<if test=\"baseActivity.status == 8 \">and ma.status = 11 and now() &gt; ma.end_time </if>  ",
            "<if test=\"baseActivity.status == 6 \">and ma.status in(1,2,4) and now() &gt; ma.start_time </if>  ",
            "</trim>",
            "</script>"
    })
    Long querySkillPageCount(@Param("baseActivity") BaseActivity baseActivity,
                                      @Param("merchantId")String merchantId,
                                      @Param("storeId")String storeId);

    @Select({
            "<script>",
            "select distinct ma.* from market_base_activity ma ",
            "left join market_coupon_trigger mct on mct.promotion_id = ma.promotion_id ",
            "left join market_coupon_config mcc on mcc.promotion_id = ma.promotion_id",
            "where 1=1 and ma.delete_flag='0' and ma.status != '10' ",
            "<trim suffixOverrides=\"and\"> ",
            "<if test=\"baseActivity.promotionId !=null and baseActivity.promotionId != '' \">and ma.promotion_id = #{baseActivity.promotionId} </if>  ",
            "<if test=\"baseActivity.promName !=null and baseActivity.promName != ''\">and ma.prom_name like CONCAT(CONCAT('%', #{baseActivity.promName}), '%') </if>  ",
            "<if test=\"baseActivity.promType !=null\">and ma.prom_type = #{baseActivity.promType} </if>  ",
            "<if test=\"baseActivity.frontPromType !=null\">and ma.front_prom_type = #{baseActivity.frontPromType} </if>  ",
            "<if test=\"baseActivity.creatorName !=null and baseActivity.creatorName != ''\">and ma.creator_name like CONCAT(CONCAT('%', #{baseActivity.creatorName}), '%') </if>  ",
            "<if test=\"baseActivity.startTime !=null\">and ma.start_time &gt;= #{baseActivity.startTime} </if>  ",
            "<if test=\"baseActivity.endTime !=null\">and ma.end_time &lt;= #{baseActivity.endTime} </if>  ",
            "<if test=\"baseActivity.channelCodes !=null\">and ma.channel_codes like CONCAT(CONCAT('%', #{baseActivity.channelCodes}), '%') </if> ",
            "<if test=\"baseActivity.deptType !=null and baseActivity.deptType == 2\"> ",
            "   and (ma.dept_id = #{baseActivity.adminOperate.deptId} or ma.promotion_id in (" +
                    "select DISTINCT a.promotion_id from market_activity_merchant_config a " +
                    "left join market_base_activity b on a.promotion_id = b.promotion_id "+
                    " where a.merchant_id=#{baseActivity.adminOperate.businessId} and a.delete_flag='0' and b.status = 11 )  "+
                    ")",
            "</if>  ",
            "<if test=\"baseActivity.deptType !=null and baseActivity.deptType == 3\"> ",
            "   and (ma.dept_id = #{baseActivity.adminOperate.deptId} or ma.promotion_id in (" +
                    "select DISTINCT a.promotion_id from market_activity_merchant_config a " +
                    "left join market_base_activity b on a.promotion_id = b.promotion_id "+
                     "where a.merchant_id=#{baseActivity.adminOperate.businessId} and a.delete_flag='0' and b.status = 11)  "+
                    ")",
            "</if> ",
            "<if test=\"baseActivity.deptType == 0\"> ",
            "and ma.dept_id = #{baseActivity.adminOperate.deptId}",
            "</if>",
            "<if test=\"baseActivity.deptType == 1\"> ",
            "and (ma.dept_id = #{baseActivity.adminOperate.deptId} or (ma.channel_codes like concat('%',#{baseActivity.adminOperate.businessId},'%') and ma.status = 11)) ",
            "</if>",
            "<if test=\"grantType != null and grantType != ''\"> ",
                "and mct.trigger_id like '%' #{grantType} '%' ",
            "</if>",
            "<if test=\"couponType != null and couponType != ''\"> ",
            "and mcc.coupon_type = #{couponType}",
            "</if>",
            "<if test=\"baseActivity.status == 9 or baseActivity.status == 7\">and ma.status = #{baseActivity.status} </if>  ",
            "<if test=\"baseActivity.status == 1 \">and ma.status = 1 and now() &lt;= ma.start_time </if>  ",
            "<if test=\"baseActivity.status == 2 \">and ma.status = 2 and now() &lt;= ma.start_time </if>  ",
            "<if test=\"baseActivity.status == 3 \">and ma.status = 11 and now() &lt;= ma.start_time </if>  ",
            "<if test=\"baseActivity.status == 4 \">and ma.status = 4 and now() &lt;= ma.start_time </if>  ",
            "<if test=\"baseActivity.status == 5 \">and ma.status = 11 and now() &gt;= ma.start_time and now() &lt;= ma.end_time </if>  ",
            "<if test=\"baseActivity.status == 8 \">and ma.status = 11 and now() &gt; ma.end_time </if>  ",
            "<if test=\"baseActivity.status == 6 \">and ma.status in(1,2,4) and now() &gt; ma.start_time </if>  ",
            "<if test=\"baseActivity.specialField == true \">and ma.status = 11 and now() &lt;= ma.end_time </if>  "
            ,
            "<if test=\"baseActivity.couponUseStartTime !=null\">and mcc.coupon_use_start_time &gt;= #{baseActivity.couponUseStartTime} </if>  ",
            "<if test=\"baseActivity.couponUseEndTime !=null\">and mcc.coupon_use_end_time &lt;= #{baseActivity.couponUseEndTime} </if>  ",
            "<if test=\"baseActivity.couponAmountStart !=null and baseActivity.couponAmountStart != '' \">and mcc.coupon_amount &gt;= #{baseActivity.couponAmountStart} </if>  ",
            "<if test=\"baseActivity.couponAmountEnd !=null and baseActivity.couponAmountEnd != '' \">and mcc.coupon_amount &lt;= #{baseActivity.couponAmountEnd} </if>  ",
            "</trim>",
            "order by ma.id desc limit #{startIndex},#{limit}",
            "</script>"
    })
    List<BaseActivity> queryCouponPage(@Param("startIndex") int startIndex,
                                      @Param("limit") int limit,
                                      @Param("baseActivity") BaseActivity baseActivity,
                                      @Param("merchantId")String merchantId,
                                      @Param("storeId")String storeId,
                                       @Param("grantType")String grantType,
                                       @Param("couponType")Integer couponType);


    @Select({
            "<script>",
            "select count(distinct ma.id) from market_base_activity ma ",
            "left join market_coupon_trigger mct on mct.promotion_id = ma.promotion_id ",
            "left join market_coupon_config mcc on mcc.promotion_id = ma.promotion_id",
            "where 1=1 and ma.delete_flag='0' and ma.status != '10' ",
            "<trim suffixOverrides=\"and\"> ",
            "<if test=\"baseActivity.promotionId !=null and baseActivity.promotionId != '' \">and ma.promotion_id = #{baseActivity.promotionId} </if>  ",
            "<if test=\"baseActivity.promName !=null and baseActivity.promName != ''\">and ma.prom_name like CONCAT(CONCAT('%', #{baseActivity.promName}), '%') </if>  ",
            "<if test=\"baseActivity.promType !=null\">and ma.prom_type = #{baseActivity.promType} </if>  ",
            "<if test=\"baseActivity.frontPromType !=null\">and ma.front_prom_type = #{baseActivity.frontPromType} </if>  ",
            "<if test=\"baseActivity.creatorName !=null and baseActivity.creatorName != ''\">and ma.creator_name like CONCAT(CONCAT('%', #{baseActivity.creatorName}), '%') </if>  ",
            "<if test=\"baseActivity.startTime !=null\">and ma.start_time &gt;= #{baseActivity.startTime} </if>  ",
            "<if test=\"baseActivity.endTime !=null\">and ma.end_time &lt;= #{baseActivity.endTime} </if>  ",
            "<if test=\"baseActivity.channelCodes !=null\">and ma.channel_codes like CONCAT(CONCAT('%', #{baseActivity.channelCodes}), '%') </if>  ",
            "<if test=\"baseActivity.deptType !=null and baseActivity.deptType == 2\"> ",
            "   and (ma.dept_id = #{baseActivity.adminOperate.deptId} or ma.promotion_id in (" +
                    "select DISTINCT a.promotion_id from market_activity_merchant_config a " +
                    "left join market_base_activity b on a.promotion_id = b.promotion_id where a.merchant_id=#{baseActivity.adminOperate.businessId} and a.delete_flag='0' and b.status = 11)  "+
                    ")",
            "</if>  ",
            "<if test=\"baseActivity.deptType !=null and baseActivity.deptType == 3\"> ",
            "   and (ma.dept_id = #{baseActivity.adminOperate.deptId} or ma.promotion_id in (" +
                    "select DISTINCT a.promotion_id from market_activity_merchant_config a " +
                    "left join market_base_activity b on a.promotion_id = b.promotion_id where a.merchant_id=#{baseActivity.adminOperate.businessId} and a.delete_flag='0' and b.status = 11)  "+
                    ")",
            "</if> ",
            "<if test=\"baseActivity.deptType == 0\"> ",
            "and ma.dept_id = #{baseActivity.adminOperate.deptId}",
            "</if>",
            "<if test=\"baseActivity.deptType == 1\"> ",
            "and (ma.dept_id = #{baseActivity.adminOperate.deptId} or (ma.channel_codes like concat('%',#{baseActivity.adminOperate.businessId},'%') and ma.status = 11)) ",
            "</if>",
            "<if test=\"grantType != null and grantType != ''\"> ",
            "   and mct.trigger_id like '%' #{grantType} '%' ",
            "</if>",
            "<if test=\"couponType != null and couponType != ''\"> ",
                "and mcc.coupon_type = #{couponType}",
            "</if>",
            "<if test=\"baseActivity.status == 9 or baseActivity.status == 7\">and ma.status = #{baseActivity.status} </if>  ",
            "<if test=\"baseActivity.status == 1 \">and ma.status = 1 and now() &lt;= ma.start_time </if>  ",
            "<if test=\"baseActivity.status == 2 \">and ma.status = 2 and now() &lt;= ma.start_time </if>  ",
            "<if test=\"baseActivity.status == 3 \">and ma.status = 11 and now() &lt;= ma.start_time </if>  ",
            "<if test=\"baseActivity.status == 4 \">and ma.status = 4 and now() &lt;= ma.start_time </if>  ",
            "<if test=\"baseActivity.status == 5 \">and ma.status = 11 and now() &gt;= ma.start_time and now() &lt;= ma.end_time </if>  ",
            "<if test=\"baseActivity.status == 8 \">and ma.status = 11 and now() &gt; ma.end_time </if>  ",
            "<if test=\"baseActivity.status == 6 \">and ma.status in(1,2,4) and now() &gt; ma.start_time </if>  ",
            "<if test=\"baseActivity.specialField == true \">and ma.status = 11 and now() &lt;= ma.end_time </if> ",


            "<if test=\"baseActivity.couponUseStartTime !=null\">and mcc.coupon_use_start_time &gt;= #{baseActivity.couponUseStartTime} </if>  ",
            "<if test=\"baseActivity.couponUseEndTime !=null\">and mcc.coupon_use_end_time &lt;= #{baseActivity.couponUseEndTime} </if>  ",
            "<if test=\"baseActivity.couponAmountStart !=null and baseActivity.couponAmountStart != '' \">and mcc.coupon_amount &gt;= #{baseActivity.couponAmountStart} </if>  ",
            "<if test=\"baseActivity.couponAmountEnd !=null and baseActivity.couponAmountEnd != '' \">and mcc.coupon_amount &lt;= #{baseActivity.couponAmountEnd} </if>  ",
            "</trim>",
            "</script>"
    })
    long queryCouponPageCount(
                             @Param("baseActivity") BaseActivity baseActivity,
                             @Param("merchantId")String merchantId,
                             @Param("storeId")String storeId,
                              @Param("grantType")String grantType,
                              @Param("couponType")Integer couponType);

    List<PromotionDataPo> queryCouponPromotionBySkuIdAndCouponCodes(@Param("skuId")Long skuId, @Param("couponCodes")List<String> couponCodes);

    List<QueryCouponCenterResponse> queryCouponCenter(@Param("promType") String promType , @Param("start") Integer start , @Param("limit") Integer limit , @Param("goodsId") String goodsId , @Param("goodsType") Integer goodsType, @Param("amountDeductType") Integer amountDeductType);

    Long queryCouponCenterTotal(@Param("promType") String promType, @Param("goodsId") String goodsId , @Param("goodsType") Integer goodsType, @Param("amountDeductType") Integer amountDeductType);

    List<QueryCouponCenterResponse> queryCouponCenterNew(@Param("promType") String promType , @Param("start") Integer start , @Param("limit") Integer limit);

    Long queryCouponCenterTotalNew(@Param("promType") String promType);


    List<CouponConfigQueryResponse> getCouponList(@Param("couponConfigQueryRequest") CouponConfigQueryRequest couponConfigQueryRequest);

    Integer getCouponListCount(@Param("couponConfigQueryRequest") CouponConfigQueryRequest couponConfigQueryRequest);

    CouponConfigQueryResponse getCouponByPromotionId(@Param("couponConfigQueryRequest") CouponConfigQueryRequest couponConfigQueryRequest);

    @Select("select * from market_base_activity where end_time >= #{startTime} and end_time < #{endTime} and status = 11 and now() > end_time")
    List<BaseActivity> queryEnd(@Param("startTime") Date startTime, @Param("endTime") Date endTime);

    @Select("select * from market_base_activity where id=#{promotionId} and delete_flag='0'")
    BaseActivity queryById(@Param("promotionId")String promotionId);
}
